<?php

include_once('logic/action/Action.php');
include_once('logic/view/manage_view.php');

class ManageAction extends Action
{
		public function perform()
		{
				$photos = new PhotosDAO;
				$tags = new TagsDAO;
				$view = new ManageView();


				if (Session::getSessionVar ("logged_in") == 'true')
				{
					$view->setParam('_user_logged_in', 'true');

					// if $tag is set, display photos tagged with $tag
					$displayAll = HttpRequest::getRequest('displayall');
					$tag = HttpRequest::getRequest('tag');

					// if "displayall" is "true" then display all entries in the database
					if ($displayAll == "true")
					{
							$entries = $photos->getAllEntries();
							foreach ($entries as $photoId)
							{
									$view->addEntry($photoId[0], $photos->getSubmitDate($photoId[0]));
							}
					}
					else if ($tag != '')
					{
							$photoIds = $tags->getTaggedPhotos($tag);
							foreach ($photoIds as $photoId)
							{
									$view->addEntry($photoId[0], $photos->getSubmitDate($photoId[0]));
							}
					}

					// add tags
					$tags = $tags->getAllTags();
					foreach ($tags as $tag)
					{
							$view->addTag($tag[0]);
					}

				} else {
					$view->setParam('_user_logged_in', 'false');
				}


				parent::setCommonParams($view);
				$view->display();
		}
}

?>
